﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.Entity;

namespace Twittit.Web
{
    public partial class _Default : System.Web.UI.Page
    {   
        static List<Twittit.DataAccess.Twit> twitList = new List<Twittit.DataAccess.Twit>();
        static int startList = 15;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] != null)
            {
                Twittit.Business.UserService t = Twittit.Business.UserService.getInstance();

                userliteral.Text = Session["username"].ToString();
                Twittit.DataAccess.User user = Twittit.Business.UserService.getInstance().getUserByUsername(Session["username"].ToString());
                int userId = user.id;

                twitList = Twittit.Business.UserService.getInstance().getTwitts(userId);
                UserTwittsRepeater.DataSource = twitList.Take(15);
                UserTwittsRepeater.DataBind();

                UsersRepeater.DataSource = Twittit.Business.UserService.getInstance().suggestUsers(userId);
                UsersRepeater.DataBind();

                SubscriptionRequestsRepeater.DataSource = Twittit.Business.UserService.getInstance().getRequests(userId);
                SubscriptionRequestsRepeater.DataBind();

                SubscribersCount.Text = Twittit.Business.UserService.getInstance().countSubscribers(userId).ToString();
                SubscribedCount.Text = Twittit.Business.UserService.getInstance().countSubscribed(userId).ToString();

                PendingRequestsCount.Text = Twittit.Business.UserService.getInstance().countPendingRequests(userId).ToString();

                UserPostCount.Text = Twittit.Business.UserService.getInstance().countUserPosts(userId).ToString();
                MentionsCount.Text = Twittit.Business.UserService.getInstance().getTaggedTwitts(userId).Count().ToString();
                UserDescriptionLiteral.Text = user.aboutyou;
                UserGravatar.Email = user.email;
            }
            else
            {
                this.Response.Redirect("Login.aspx");
            }
        }

        [WebMethod(EnableSession = true)]
        public static String listTwitts()
        {
            string result = "";
            if (HttpContext.Current.Session["username"] != null)
            {
                Twittit.DataAccess.User user = Twittit.Business.UserService.getInstance().getUserByUsername(HttpContext.Current.Session["username"].ToString());
                int userId = user.id;
                List<Twittit.DataAccess.Twit> next = (List<Twittit.DataAccess.Twit>)twitList.Skip(startList).Take(15).ToList();
                foreach (Twittit.DataAccess.Twit t in next)
                {
                    String delete = "";
                    if (t.id == userId)
                        delete = "<a class=\"delete\" twittid=\"" + t.id + "\" href=\"#\"><img src=\"/Images/delete.png\" />Supprimer</a>";
                    result += "<div class=\"twitt\">" +
						"<span class=\"avatar\">" +
                            "<img  height=\"50px\" src=\"" + t.User.getGravatarURL() + "\" />" +
						"</span>" +
						"<div class=\"ctrl\">" +
                            "<a href=\"#\"><img src=\"/Images/answer.png\" twittid=\"" + t.id + "\" />Repondre</a>"+
							"<a href=\"#\" class=\"retwitt\" twittid=\""+t.id+"\"><img src=\"/Images/forward.png\" />Retwittiter</a>" +
							 delete+
						"</div>" +

						"<span class=\"author\">" +
							"<a href=\"Profile.aspx?user="+t.user_id+"\">"+t.User + "</a>"+
						"</span>" +
						"<span class=\"content\">" +
							t.mixed +
						"</span>" +
                        "<span class=\"date\">" +
							t.date +
						"</span>" +
					"</div>";
                }
            }
            startList += 15;
            return result;
        }

        [WebMethod(EnableSession = true)]
        public static String addTwitt(String text)
        {
            if (HttpContext.Current.Session["username"] != null && text.Length > 2)
            {
                string result = "";
                startList = 0;

                Twittit.DataAccess.User user = Twittit.Business.UserService.getInstance().getUserByUsername(HttpContext.Current.Session["username"].ToString());
                int userId = user.id;
                Twittit.DataAccess.Twit twit = Twittit.Business.UserService.getInstance().addTwitt(userId, text);

                twitList = Twittit.Business.UserService.getInstance().getTwitts(userId);
                
                List<Twittit.DataAccess.Twit> next = (List<Twittit.DataAccess.Twit>)twitList.Skip(startList).Take(15).ToList();
                    foreach (Twittit.DataAccess.Twit t in next)
                    {
                        String delete = "";
                        if (t.user_id == userId) delete = "<a class=\"delete\" twittid=\"" + t.id + "\" href=\"#\"><img src=\"/Images/delete.png\" />Supprimer</a>";
                        result += "<div class=\"twitt\">" +
                            "<span class=\"avatar\">" +
                                "<img  height=\"50px\" src=\"" + t.User.getGravatarURL() + "\" />" +
                               "</span>" +
                               "<div class=\"ctrl\">" +
                                   "<a href=\"#\"><img src=\"/Images/answer.png\" twittid=\"" + t.id + "\" />Repondre</a>" +
                                   "<a href=\"#\" class=\"retwitt\" twittid=\"" + t.id + "\"><img src=\"/Images/forward.png\" />Retwittiter</a>" +
                                    delete +
                               "</div>" +

                               "<span class=\"author\">" +
                                   "<a href=\"Profile.aspx?user=" + t.user_id + "\">" + t.User + "</a>"+
                               "</span>" +
                               "<span class=\"content\">" +
                                   t.mixed +
                               "</span>" +
                               "<span class=\"date\">" +
                                   t.date +
                               "</span>" +
                           "</div>";
                    
                }

                startList += 15;
                return result;
            }
            return "";
        }

        [WebMethod(EnableSession = true)]
        public static String listPublicTwitts()
        {
            if (HttpContext.Current.Session["username"] != null)
            {
                string result = "";
                startList = 0;

                Twittit.DataAccess.User user = Twittit.Business.UserService.getInstance().getUserByUsername(HttpContext.Current.Session["username"].ToString());
                int userId = user.id;

                twitList = Twittit.Business.UserService.getInstance().getTwitts(userId);

                List<Twittit.DataAccess.Twit> next = (List<Twittit.DataAccess.Twit>)twitList.Skip(startList).Take(15).ToList();
                foreach (Twittit.DataAccess.Twit t in next)
                {
                    String delete = "";
                    if (t.user_id == userId) delete = "<a class=\"delete\" twittid=\"" + t.id + "\" href=\"#\"><img src=\"/Images/delete.png\" />Supprimer</a>";
                    result += "<div class=\"twitt\">" +
                        "<span class=\"avatar\">" +
                            "<img  height=\"50px\" src=\"" + t.User.getGravatarURL() + "\" />" +
                           "</span>" +
                           "<div class=\"ctrl\">" +
                               "<a href=\"#\"><img src=\"/Images/answer.png\" twittid=\"" + t.id + "\" />Repondre</a>" +
                               "<a href=\"#\" class=\"retwitt\" twittid=\"" + t.id + "\"><img src=\"/Images/forward.png\" />Retwittiter</a>" +
                                delete +
                           "</div>" +

                           "<span class=\"author\">" +
                               "<a href=\"Profile.aspx?user=" + t.user_id + "\">" + t.User + "</a>"+
                           "</span>" +
                           "<span class=\"content\">" +
                               t.mixed +
                           "</span>" +
                           "<span class=\"date\">" +
                               t.date +
                           "</span>" +
                       "</div>";

                }

                startList += 15;
                return result;
            }
            return "";
        }

        [WebMethod(EnableSession = true)]
        public static String listPrivateTwitts()
        {
            string result = "";
            if (HttpContext.Current.Session["username"] != null)
            {
                Twittit.DataAccess.User user = Twittit.Business.UserService.getInstance().getUserByUsername(HttpContext.Current.Session["username"].ToString());
                int userId = user.id;
                List<Twittit.DataAccess.Twit> next = Twittit.Business.UserService.getInstance().getTaggedTwitts(userId);
                if (next == null)
                    return "";
                foreach (Twittit.DataAccess.Twit t in next)
                {
                    String delete = "";
                    if (t.id == userId)
                        delete = "<a class=\"delete\" twittid=\"" + t.id + "\" href=\"#\"><img src=\"/Images/delete.png\" />Supprimer</a>";
                    result += "<div class=\"twitt\">" +
                        "<span class=\"avatar\">" +
                            "<img  height=\"50px\" src=\"" + t.User.getGravatarURL() + "\" />" +
                        "</span>" +
                        "<div class=\"ctrl\">" +
                            "<a href=\"#\"><img src=\"/Images/answer.png\" twittid=\"" + t.id + "\" />Repondre</a>" +
                            "<a href=\"#\" class=\"retwitt\" twittid=\"" + t.id + "\"><img src=\"/Images/forward.png\" />Retwittiter</a>" +
                             delete +
                        "</div>" +

                        "<span class=\"author\">" +
                            "<a href=\"Profile.aspx?user=" + t.user_id + "\">" + t.User + "</a>" +
                        "</span>" +
                        "<span class=\"content\">" +
                            t.mixed +
                        "</span>" +
                        "<span class=\"date\">" +
                            t.date +
                        "</span>" +
                    "</div>";
                }
            }
            return result;
        }

        [WebMethod(EnableSession = true)]
        public static String autocomplete(String username)
        {
            String result =  "";
            List<Twittit.DataAccess.User> users = Twittit.Business.UserService.getInstance().searchUser(username);

            foreach (Twittit.DataAccess.User u in users)
            {
                result += "<li>" + u.twittit + "</li>";
            }

            return result;
        }

        [WebMethod(EnableSession = true)]
        public static String autocompleteSearch(String username)
        {
            String result = "";
            List<Twittit.DataAccess.User> users = Twittit.Business.UserService.getInstance().searchUser(username);
            List<Twittit.DataAccess.Tag> tags = Twittit.Business.TagService.getInstance().getTags(username);
            foreach (Twittit.DataAccess.User u in users)
            {
                result += "<a class=\"user\" href=\"Profile.aspx?user="+u.id+"\">" + u.twittit + "</a>";
            }
            foreach (Twittit.DataAccess.Tag t in tags)
            {
                result += "<a class=\"tag\" href=\"Tag.aspx?value=" + t.tag1 + "\">" + t.tag1 + "</a>";
            }

            return result;
        }

        [WebMethod(EnableSession = true)]
        public static String subscribeTo(int user)
        {
            Boolean result = false;
            if (HttpContext.Current.Session["username"] != null)
            {
                int subscriber = Twittit.Business.UserService.getInstance().getUserByUsername(HttpContext.Current.Session["username"].ToString()).id;

                if (Twittit.Business.UserService.getInstance().hasSubscribed(subscriber, user))
                {
                    Twittit.Business.UserService.getInstance().unSubscribe(subscriber, user);
                    result = false;
                }
                else
                {
                    Twittit.Business.UserService.getInstance().subscribe(subscriber, user);
                    result = true;
                }
            }
            return result.ToString();
        }

        [WebMethod(EnableSession = true)]
        public static String deleteTwitt(int twittId)
        {
            Boolean result = false;
            if (HttpContext.Current.Session["username"] != null)
            {
                int userId = Twittit.Business.UserService.getInstance().getUserByUsername(HttpContext.Current.Session["username"].ToString()).id;
                int success = Twittit.Business.TwittService.getInstance().removeTwitt(twittId,userId);
                if (success == 0)
                    result = true;
            }
            return result.ToString();
        }

        [WebMethod(EnableSession = true)]
        public static String acceptRequest(int subscriber)
        {
            Boolean result = false;
            if (HttpContext.Current.Session["username"] != null)
            {
                int user = Twittit.Business.UserService.getInstance().getUserByUsername(HttpContext.Current.Session["username"].ToString()).id;
                int success = Twittit.Business.UserService.getInstance().acceptRequest(subscriber, user);
                if (success == 0)
                    result = true;
            }
            return result.ToString();
        }

        [WebMethod(EnableSession = true)]
        public static String denyRequest(int subscriber)
        {
            Boolean result = false;
            if (HttpContext.Current.Session["username"] != null)
            {
                int user = Twittit.Business.UserService.getInstance().getUserByUsername(HttpContext.Current.Session["username"].ToString()).id;
                int success = Twittit.Business.UserService.getInstance().denyRequest(subscriber, user);
                if (success == 0)
                    result = true;
            }
            return result.ToString();
        }

        protected void TwittitButton_Click1(object sender, EventArgs e)
        {
            String content = TwittTextarea.Text;
            int userId = Twittit.Business.UserService.getInstance().getUserByUsername(Session["username"].ToString()).id;
            if (TwittitTextareaValidator.IsValid)
                Twittit.Business.UserService.getInstance().addTwitt(userId, content);
        }

    }
}
